/**
 * @package: BrickCAD
 * @className: DimensionView.java
 * @package: default
 * @author: Sean , Jack, Lester, Erik Telstad
 * @date_created: November 16, 2009
 * @last_motified: November 16, 2009
 * 
 * 
 * 
 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
//import javax.swing.text.BadLocationException;


public class DimensionView extends BrickView implements ActionListener {

	private static final long serialVersionUID = 1L;
	private static String title = "Dimension View";
	static int openFrameCount = 0;
	static final int xOffset = 400, yOffset = 10;
	
	private CommandProcessor cmmdPr;
	
    private static final String lengthFieldString = "LengthField";
    private static final String heightFieldString = "HeightField";
    private static final String widthFieldString = "WidthField";


	private JLabel lengthLabel, heightLabel, widthLabel, instructLabel;
	private JTextField lengthTextField, heightTextField, widthTextField;

	/**
	 * @param theModel
	 */
	public DimensionView(Brick theModel) {
		super(theModel);
		setTitle(title);
		openFrameCount++;
		//cmmdPr = cp;
		
		lengthLabel = new JLabel("Length:");
		widthLabel = new JLabel("Width:");
		heightLabel = new JLabel("Height:");
		instructLabel = new JLabel("Type new value in field and press Enter.");
		
		lengthTextField = new JTextField(10);
		lengthTextField.setActionCommand(lengthFieldString);
		lengthTextField.addActionListener(this);
		
		widthTextField = new JTextField(10);
		widthTextField.setActionCommand(widthFieldString);
		widthTextField.addActionListener(this);
		
		heightTextField = new JTextField(10);
		heightTextField.setActionCommand(heightFieldString);
		heightTextField.addActionListener(this);
		
		// saw a nice layout in the Java Tutorial, ...so I took it!
		GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        contentPane.setLayout(gridbag);
        
        JLabel[] labels = {lengthLabel, heightLabel, widthLabel};
        JTextField[] textFields = {lengthTextField, heightTextField, widthTextField};
        addLabelTextRows(labels, textFields, gridbag, contentPane);

        c.gridwidth = GridBagConstraints.REMAINDER; //last
        c.anchor = GridBagConstraints.WEST;
        c.weightx = 1.0;
        contentPane.add(instructLabel, c);
        contentPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Brick Attributes"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));

        

		this.add(contentPane, BorderLayout.CENTER);
		
		this.lengthTextField.setText(Double.toString(theModel.getLength()));
		this.widthTextField.setText(Double.toString(theModel.getWidth()));
		this.heightTextField.setText(Double.toString(theModel.getHeight()));
		
		setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
		pack();
		
	}

    /**
     * a method to set up the contentPane layout - borrowed from Java Tutorials
     * @param labels
     * @param textFields
     * @param gridbag
     * @param container
     */
    private void addLabelTextRows(JLabel[] labels,
            					  JTextField[] textFields,
            					  GridBagLayout gridbag,
            					  Container container) {
    	
    	GridBagConstraints c = new GridBagConstraints();
    	c.anchor = GridBagConstraints.CENTER;
    	int numLabels = labels.length;

    	for (int i = 0; i < numLabels; i++) {
    		c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last
    		c.fill = GridBagConstraints.NONE;      //reset to default
    		c.weightx = 0.0;                       //reset to default
    		container.add(labels[i], c);

    		c.gridwidth = GridBagConstraints.REMAINDER;     //end row
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 1.0;
    		container.add(textFields[i], c);
    	}
    }

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			if(lengthFieldString.equals(e.getActionCommand()))
				AppFrame.cmmdProc.execute(new SetLengthCommand(Double.parseDouble(lengthTextField.getText())));
				//model.setLength(Double.parseDouble(lengthTextField.getText()));
				
			if (heightFieldString.equals(e.getActionCommand()))
				AppFrame.cmmdProc.execute(new SetHeightCommand(Double.parseDouble(heightTextField.getText())));
//				model.setHeight(Double.parseDouble(heightTextField.getText()));
				
			if (widthFieldString.equals(e.getActionCommand()))
				AppFrame.cmmdProc.execute(new SetWidthCommand(Double.parseDouble(widthTextField.getText())));
//				model.setWidth(Double.parseDouble(widthTextField.getText()));
			
		} catch(NumberFormatException nfe) {
			JOptionPane.showMessageDialog(this, nfe.getMessage());
		} catch(IllegalArgumentException iae) {
			JOptionPane.showMessageDialog(this, iae.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see BrickView#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable arg0, Object o) {
		//System.out.println("Updating DimensionView #" + openFrameCount);
		model = (Brick) arg0;
		
		//System.out.print("Brick length: " + model.getLength());
		this.lengthTextField.setText(Double.toString(model.getLength()));
		
		//System.out.print("/Brick height: " + model.getHeight());
		this.heightTextField.setText(Double.toString(model.getHeight()));
		
		//System.out.print("/Brick width: " + model.getWidth());
		this.widthTextField.setText(Double.toString(model.getWidth()));
	}
}
